package com.gewe.wxChat.controller;


import com.alibaba.fastjson2.JSON;
import com.gewe.wxChat.model.dto.message.MessageDto;
import com.gewe.wxChat.netty.PushMsgService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private PushMsgService pushMsgService;

    @PostMapping("/pushToOne")
    public String pushMsgToOne(@RequestParam String deviceWxId) {
        try {
            List<MessageDto> list = new ArrayList<>();
            for (int i = 0; i < 5; i++){
                MessageDto messageDto = new MessageDto();
                messageDto.setContent("测试内容"+i);
                messageDto.setTime("3112");
                messageDto.setSenderWxId("1212221");
                messageDto.setSenderUserName("测试账号"+i);
                list.add(messageDto);
            }
            for (int i = 0; i < 5; i++){
                MessageDto messageDto = new MessageDto();
                messageDto.setContent("测试内容"+i);
                messageDto.setTime("3112");
                messageDto.setSenderWxId("1212221");
                messageDto.setSenderUserName("测试账号"+i);
                messageDto.setSenderRoomId("3"+i);
                messageDto.setSenderRoomName("第"+i+"群");
                list.add(messageDto);
            }
            String msg = JSON.toJSONString(list);
            pushMsgService.pushMsgToOne(deviceWxId, msg);
            return "Message sent to user: " + deviceWxId;
        } catch (RuntimeException e) {
            log.error("Failed to send message to user: " + deviceWxId, e);
            return "Failed to send message to user: " + deviceWxId + ". Error: " + e.getMessage();
        }
    }

    @PostMapping("/pushToAll")
    public String pushMsgToAll(@RequestParam String msg) {
        try {
            pushMsgService.pushMsgToAll(msg);
            return "Message sent to all users";
        } catch (RuntimeException e) {
            log.error("Failed to send message to all users", e);
            return "Failed to send message to all users. Error: " + e.getMessage();
        }
    }
}

